package com.util;

import com.aspose.words.Document;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Map;

/**
 * @author Smile
 */
public class WordUtil {

    /**
     * word转换PDF
     *
     * @param wordBytes
     * @return
     * @throws Exception
     */
    public static byte[] word2Pdf(byte[] wordBytes) throws Exception {
        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
            try (ByteArrayInputStream is = new ByteArrayInputStream(wordBytes)) {
                Document doc = new Document(is);
                // 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
                doc.save(os, com.aspose.words.SaveFormat.PDF);
                return os.toByteArray();
            }
        }
    }

    /**
     * word转换image
     *
     * @param wordBytes
     * @return
     * @throws Exception
     */
    public static Map<Integer, String> word2Image(byte[] wordBytes) throws Exception {
        byte[] pdfBytes = word2Pdf(wordBytes);
        return PdfUtil.Pdf2Image(pdfBytes);
    }
}
